package ru.papergames.battleserver.network.serverpackets.flashuser;

import org.apache.log4j.Logger;
import org.mmocore.network.SendablePacket;
import ru.papergames.battleserver.network.PaperGameClient;

/**
 * @author: ATroshanin
 * Date: 15.06.2010
 * Time: 14:14:09
 */
public abstract class PaperGameServerPacket extends SendablePacket<PaperGameClient> implements PaperGameServerPacketTypes {
    private static final Logger _log = Logger.getLogger(PaperGameServerPacket.class.getName());

    protected boolean _invisible = false;

    /**
     * @return True if packet originated from invisible character.
     */
    public boolean isInvisible() {
        return _invisible;
    }

    /**
     * Set "invisible" boolean flag in the packet.
     * Packets from invisible characters will not be broadcasted to players.
     *
     * @param b
     */
    public void setInvisible(boolean b) {
        _invisible = b;
    }

    /**
     * @see org.mmocore.network.SendablePacket#write()
     */
    @Override
    protected void write() {
        try {
            writePacketId();
            //_log.info(this.getType());
            writePacketData();
        }
        catch (Exception e) {
            _log.error("Client: " + getClient().toString() + " - Failed writing: " + getType());
            e.printStackTrace();
        }
    }

    public void runImpl() {

    }

    protected void writePacketId() {
        writeC(getPacketId());
    }

    protected abstract int getPacketId();

    protected abstract void writePacketData();

    public int getPacketIdValue() {
        return getPacketId();
    }

    /**
     * @return A String with this packet name for debuging purposes
     */
    public String getType() {
        return "[s] " + this.getClass() + " [" + getPacketId() + "]";
    }

}
